# Contributor: Damian Kurek <starfire24680@gmail.com>
# Maintainer: Leon Marz <main@lmarz.org>
pkgname=osl
pkgver=1.12.14.0
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers"
options="!check" # All tests fail
url="https://opensource.imageworks.com/osl.html"
# riscv64 and s390x blocked by openimageio
arch="all !riscv64 !s390x"
license="BSD-3-Clause"
_llvmver=15
makedepends="cmake
	bison
	boost-dev
	clang$_llvmver-dev
	flex-dev
	fmt-dev
	lld
	llvm$_llvmver-dev
	llvm$_llvmver-libs
	openexr-dev
	openimageio-dev
	openimageio-tools
	pugixml-dev
	python3-dev
	py3-pybind11-dev
	qt6-qtbase-dev
	samurai
	zlib-dev
	"
subpackages="
	$pkgname-libs
	$pkgname-dev
	$pkgname-doc
	$pkgname-python
	openimageio-plugin-osl:_osl
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/imageworks/OpenShadingLanguage/archive/v$pkgver.tar.gz
	armv7-neon.patch.noauto
	"
builddir="$srcdir/OpenShadingLanguage-$pkgver"

prepare() {
	default_prepare

	case "$CARCH" in
	armv7)
		patch -Np1 < "$srcdir"/armv7-neon.patch.noauto
		;;
	esac
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi

	case "$CARCH" in
	armv7)
		export CXXFLAGS="$CXXFLAGS -DOIIO_NO_NEON"
		;;
	esac

	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_STANDARD=14 \
		-DCMAKE_SKIP_RPATH=ON \
		-DLLVM_ROOT="/usr/lib/llvm$_llvmver" \
		-DLLVM_BC_GENERATOR="/usr/bin/clang++-$_llvmver" \
		-DLLVM_AS_TOOL="/usr/lib/llvm$_llvmver/bin/llvm-as" \
		-DLLVM_LINK_TOOL="/usr/lib/llvm$_llvmver/bin/llvm-link" \
		-DLLVM_STATIC=OFF \
		-DOSL_BUILD_TESTS="$(want_check && echo ON || echo OFF)" \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	cd build
	# Broken tests
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

python() {
	pkgdesc="Python3 bindings for OSL"
	depends=""
	amove usr/lib/python3*
}

doc() {
	default_doc
	# Example shaders
	amove usr/share/OSL/shaders/*.osl
	amove usr/share/OSL/shaders/*.oso
}

_osl() {
	pkgdesc="Plugin to access OSL from OpenImageIO"
	depends=""
	amove usr/lib/osl.imageio.so
}

sha512sums="
51641e58fb0bba042853e3976c104decc788f1b1d3ec2e33a3f5504b11b05950dc38766ffb066b0f76edc4e0a8829d6b7dd408d9288b74d69380395b1d991bd5  osl-1.12.14.0.tar.gz
6083b35af111ae357cf73d364df3043bcd67430481961cc4ceed2d0055691b4dddc58bc945dd0d122ed0bf54b4577a9d1dad58da41af0472befec3a12606b11d  armv7-neon.patch.noauto
"
